<template>
  <div class="user-list">
    <div class="user-list-header">
      <h3>在线用户 ({{ onlineCount }})</h3>
    </div>
    <div class="user-list-content">
      <div 
        v-for="user in users" 
        :key="user.nick_name"
        class="user-item"
        :class="{ 
          'current-user': user.uid === currentUser.uid,
          'selected': selectedUser && user.uid === selectedUser.uid  
        }"
        @click="handleUserClick(user)"
      >
      <div class="user-avatar">
        <img 
          v-if="user.head_img && isValidUrl(user.head_img)" 
          :src="user.head_img" 
          :alt="user.nick_name"
        />
        <span v-else>{{ getInitials(user.nick_name) }}</span>
      </div>
        <div class="user-info">
          <div class="user-name">
            {{ user.nick_name }}
            <span v-if="user.nick_name === currentUser.nick_name">(我)</span>
          </div>
          <div class="user-status" :class="{ 'status-online': user.online }">
            {{ user.online ? '在线' : '离线' }}
          </div>
        </div>
      </div>
      <div v-if="!users.length" class="no-users">
        暂无在线用户~
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { userService } from '@/services/userService'

const props = defineProps({
  users: {
    type: Array,
    default: () => []
  },
  selectedUser: {
    type: Object,
    default: null
  }
})

const currentUser = computed(() => userService.getUser())
console.log('current:',currentUser)

// 计算在线用户数量
const onlineCount = computed(() => {
  console.log('当前用户列表:', props.users)
  return props.users.filter(user => user.online).length
})

const isValidUrl = (url) => {
  try {
    new URL(url)
    return true
  } catch {
    return false
  }
}

const getInitials = (name) => {
  if (!name) return ''
  return name.charAt(0).toUpperCase()
}

const emit = defineEmits(['select-user'])

const handleUserClick = (user) => {
  console.log('currentUser:',currentUser)
  console.log('user:',user)
  if (user.uid !== currentUser.uid) {
    
    emit('select-user', user)
  }
}
</script>

<style scoped>
.user-list {
  width: 250px;
  border-right: 1px solid #eee;
  background-color: #fff;
  display: flex;
  flex-direction: column;
}

.user-list-header {
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.user-list-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
}

.user-list-content {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

.user-item {
  display: flex;
  align-items: center;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 5px;
}

.user-item:hover {
  background-color: #f5f5f5;
}

.current-user {
  background-color: #e8f5e9;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #07c160;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 10px;
  overflow: hidden;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-avatar span {
  font-size: 18px;
}

.user-info {
  flex: 1;
  overflow: hidden;
}

.user-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 4px;
}

.user-name span {
  font-size: 12px;
  color: #666;
}

.user-status {
  font-size: 12px;
  color: #999;
  display: flex;
  align-items: center;
}

.user-status::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #ccc;
  margin-right: 5px;
  transition: background-color 0.3s ease;
}

.status-online::before {
  background-color: #07c160;
}

.no-users {
  text-align: center;
  color: #999;
  padding: 20px;
  font-size: 14px;
}

/* 添加动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.user-item {
  animation: fadeIn 0.3s ease-out;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-list {
    width: 200px;
  }
}

@media (max-width: 576px) {
  .user-list {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid #eee;
  }
}
</style>